package com.navi.loaddata.loadService.impl.mesData;

import com.navi.loaddata.core.gpEntity.File.FileDefectPojo;
import com.navi.loaddata.core.gpEntity.File.FileGlassPojo;
import com.navi.loaddata.core.gpEntity.hisToGP.MCaldD;
import com.navi.loaddata.core.repository.impl.GreenPlumRepositoryImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoadMESDataBean {

    @Autowired
    GreenPlumRepositoryImpl mprodDRepository;

    public void setDefectHourSht(FileDefectPojo defectInfo) {
        String date = defectInfo.getEvtTimestamp().replace(" ","").replace("-","").replace(":","");
        String hour = date.substring(8,14);
        int hourint = Integer.parseInt(hour);

        if(hourint>=0 && hourint<10000)defectInfo.setHourKeyFk("1");
        if(hourint>=10000 && hourint<20000)defectInfo.setHourKeyFk("2");
        if(hourint>=20000 && hourint<30000)defectInfo.setHourKeyFk("3");
        if(hourint>=30000 && hourint<40000)defectInfo.setHourKeyFk("4");
        if(hourint>=40000 && hourint<50000)defectInfo.setHourKeyFk("5");
        if(hourint>=50000 && hourint<60000)defectInfo.setHourKeyFk("6");
        if(hourint>=60000 && hourint<70000)defectInfo.setHourKeyFk("7");
        if(hourint>=70000 && hourint<80000)defectInfo.setHourKeyFk("8");
        if(hourint>=80000 && hourint<90000)defectInfo.setHourKeyFk("9");
        if(hourint>=90000 && hourint<100000)defectInfo.setHourKeyFk("10");
        if(hourint>=100000 && hourint<110000)defectInfo.setHourKeyFk("11");
        if(hourint>=110000 && hourint<120000)defectInfo.setHourKeyFk("12");
        if(hourint>=120000 && hourint<130000)defectInfo.setHourKeyFk("13");
        if(hourint>=130000 && hourint<140000)defectInfo.setHourKeyFk("14");
        if(hourint>=140000 && hourint<150000)defectInfo.setHourKeyFk("15");
        if(hourint>=150000 && hourint<160000)defectInfo.setHourKeyFk("16");
        if(hourint>=160000 && hourint<170000)defectInfo.setHourKeyFk("17");
        if(hourint>=170000 && hourint<180000)defectInfo.setHourKeyFk("18");
        if(hourint>=180000 && hourint<190000)defectInfo.setHourKeyFk("19");
        if(hourint>=190000 && hourint<200000)defectInfo.setHourKeyFk("20");
        if(hourint>=200000 && hourint<210000)defectInfo.setHourKeyFk("21");
        if(hourint>=210000 && hourint<220000)defectInfo.setHourKeyFk("22");
        if(hourint>=220000 && hourint<230000)defectInfo.setHourKeyFk("23");
        if(hourint>=230000 && hourint<235959)defectInfo.setHourKeyFk("24");
        defectInfo.setHourId(String.valueOf(Integer.parseInt(defectInfo.getHourKeyFk())-1));
        String datecald =date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8);
        if (hourint<83000) {
            DateTime caldDate = new DateTime(datecald);
            datecald = caldDate.minusDays(1).toString("yyyy-MM-dd");
        }

        String calSQLKey = "From MCaldD where caldDate='" + datecald + "'";
        List<MCaldD> caldList = mprodDRepository.find(MCaldD.class,calSQLKey);
        defectInfo.setCaldDateKeyFk(String.valueOf(caldList.get(0).getCaldKey()));
        defectInfo.setCaldDate(datecald);

        if(hourint>=83000 && hourint<203000){
            if ("ARY".equals(defectInfo.getShopId())){
                defectInfo.setShfId("10AD");
                defectInfo.setShfKeyFk("1");
            }else if("CF".equals(defectInfo.getShopId())){
                defectInfo.setShfId("10CD");
                defectInfo.setShfKeyFk("3");
            }else {
                defectInfo.setShfId("10LD");
                defectInfo.setShfKeyFk("5");
            }
        }else{
            if ("ARY".equals(defectInfo.getShopId())){
                defectInfo.setShfId("10AN");
                defectInfo.setShfKeyFk("2");
            }else if("CF".equals(defectInfo.getShopId())){
                defectInfo.setShfId("10CN");
                defectInfo.setShfKeyFk("4");
            }else {
                defectInfo.setShfId("10LF");
                defectInfo.setShfKeyFk("6");
            }
        }
    }



    public void setGlassHourSht(FileGlassPojo glassInfo) {
        String date = glassInfo.getEvtTimestamp().replace(" ","").replace("-","").replace(":","");
        String hour = date.substring(8,14);
        int hourint = Integer.parseInt(hour);

        if(hourint>=0 && hourint<10000)glassInfo.setHourKeyFk("1");
        if(hourint>=10000 && hourint<20000)glassInfo.setHourKeyFk("2");
        if(hourint>=20000 && hourint<30000)glassInfo.setHourKeyFk("3");
        if(hourint>=30000 && hourint<40000)glassInfo.setHourKeyFk("4");
        if(hourint>=40000 && hourint<50000)glassInfo.setHourKeyFk("5");
        if(hourint>=50000 && hourint<60000)glassInfo.setHourKeyFk("6");
        if(hourint>=60000 && hourint<70000)glassInfo.setHourKeyFk("7");
        if(hourint>=70000 && hourint<80000)glassInfo.setHourKeyFk("8");
        if(hourint>=80000 && hourint<90000)glassInfo.setHourKeyFk("9");
        if(hourint>=90000 && hourint<100000)glassInfo.setHourKeyFk("10");
        if(hourint>=100000 && hourint<110000)glassInfo.setHourKeyFk("11");
        if(hourint>=110000 && hourint<120000)glassInfo.setHourKeyFk("12");
        if(hourint>=120000 && hourint<130000)glassInfo.setHourKeyFk("13");
        if(hourint>=130000 && hourint<140000)glassInfo.setHourKeyFk("14");
        if(hourint>=140000 && hourint<150000)glassInfo.setHourKeyFk("15");
        if(hourint>=150000 && hourint<160000)glassInfo.setHourKeyFk("16");
        if(hourint>=160000 && hourint<170000)glassInfo.setHourKeyFk("17");
        if(hourint>=170000 && hourint<180000)glassInfo.setHourKeyFk("18");
        if(hourint>=180000 && hourint<190000)glassInfo.setHourKeyFk("19");
        if(hourint>=190000 && hourint<200000)glassInfo.setHourKeyFk("20");
        if(hourint>=200000 && hourint<210000)glassInfo.setHourKeyFk("21");
        if(hourint>=210000 && hourint<220000)glassInfo.setHourKeyFk("22");
        if(hourint>=220000 && hourint<230000)glassInfo.setHourKeyFk("23");
        if(hourint>=230000 && hourint<235959)glassInfo.setHourKeyFk("24");
        glassInfo.setHourId(String.valueOf(Integer.parseInt(glassInfo.getHourKeyFk())-1));
        String datecald =date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8);
        if (hourint<83000) {
            DateTime caldDate = new DateTime(datecald);
            datecald = caldDate.minusDays(1).toString("yyyy-MM-dd");
        }

        String calSQLKey = "From MCaldD where caldDate='" + datecald + "'";
        List<MCaldD> caldList = mprodDRepository.find(MCaldD.class,calSQLKey);
        glassInfo.setCaldDateKeyFk(String.valueOf(caldList.get(0).getCaldKey()));
        glassInfo.setCaldDate(datecald);

        if(hourint>=83000 && hourint<203000){
            if ("ARY".equals(glassInfo.getShopId())){
                glassInfo.setShfId("10AD");
                glassInfo.setShfKeyFk("1");
            }else if("CF".equals(glassInfo.getShopId())){
                glassInfo.setShfId("10CD");
                glassInfo.setShfKeyFk("3");
            }else {
                glassInfo.setShfId("10LD");
                glassInfo.setShfKeyFk("5");
            }
        }else{
            if ("ARY".equals(glassInfo.getShopId())){
                glassInfo.setShfId("10AN");
                glassInfo.setShfKeyFk("2");
            }else if("CF".equals(glassInfo.getShopId())){
                glassInfo.setShfId("10CN");
                glassInfo.setShfKeyFk("4");
            }else {
                glassInfo.setShfId("10LF");
                glassInfo.setShfKeyFk("6");
            }
        }
    }
}
